interface State {
  aimDom: {
    style?: { [organName: string]: string };
    context?: { value: string; type: string };
    faPosition?: { top: number; left: number };
    [organName: string]: any;
  }; //目标DOM节点
  aimPath: {}[]; //目标路径
  aimAttrs: {
    context?: string;
    goto?: { type: string; value: string };
    [organName: string]: any;
  }; //目标属性
  aimBox: { attrs?: { class: string } }; //目标容器
  aimXY: { [organName: number]: number }; //鼠标与组件的偏移
  componentList: { [organName: string]: {} };
  seniorBoxList: {};
  //为了可以显示margin...
  colorDomStyle: {
    width: string;
    height: string;
    top: string;
    left: string;
    padding: string;
    border: string;
    margin: string;
  };
  judgeColorDom: boolean;
  domStack: {
    type: string;
    pageName: string;
    data: (string | number | {})[];
    path: {}[][];
  }[]; //存储栈
  domStackNum: number; //存储栈控制下标
  page?: {
    tree: {
      [organName: string]: [];
    };
  };
}

const fs = window.require("fs");

export default function storeData(
  state: State,
  {
    that,
  }: {
    that: {
      imgList: { blob: string; src: string }[];
      __filePath: string;
      $message: any;
    };
  }
) {
  let data = JSON.stringify(state.page?.tree);

  //对图片进行替换
  for (const key in that.imgList) {
    if (Object.hasOwnProperty.call(that.imgList, key)) {
      data = data.replace(that.imgList[key].blob, that.imgList[key].src);
    }
  }

  //写入文件
  try {
    fs.writeFileSync(that.__filePath + "/pageData.json", data);
    that.$message.success("保存成功");
  } catch (error) {
    that.$message.error("保存失败，请重试!");
    console.error(error);
  }
}
